import ServiceBase from '../base';
import {FilePageProxy} from '../proxies/file-page-proxy';
import {FilePathProxy} from '../proxies/file-path-proxy';
import {FileProxy} from '../proxies/file-proxy';
import {FolderProxy} from '../proxies/folder-proxy';
import * as proxy from '../proxy';
import {UPLOAD_REQUEST_TIMEOUT} from '../context/context-base';

const GET_FILES = '/accounts';
const PROJECTS = '/projects';

class FilesService extends ServiceBase {
    getFolderPath = (data) => {
        let promise = super.get({
            url: `/accounts/${data.accountId}/path`,
            params: data.data,
        });
        return proxy.httpResultProxy(promise, FilePathProxy);
    };
    getFiles = (data) => {
        let promise = super.get({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/folders/${data.folderId}`,
        });
        return proxy.httpResultProxy(promise, FilePageProxy);
    };
    postFolder = (data) => {
        let promise = super.post({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/parent/${data.folderId}/folder`,
            data: data.body,
        });
        return proxy.httpResultProxy(promise, FolderProxy);
    };
    putFolder = (data) => {
        let promise = super.put({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/folders/${data.folderId}`,
            data: data.body,
        });
        return proxy.httpResultProxy(promise, FolderProxy);
    };
    moveFolder = (data) => {
        let promise = super.put({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/parent/${data.parentId}/folder`,
            data: data.body,
        });
        return proxy.httpResultProxy(promise, FolderProxy);
    };
    putFile = (data) => {
        let promise = super.put({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/files/${data.fileId}`,
            data: data.body,
        });
        return proxy.httpResultProxy(promise, FileProxy);
    };
    deleteFolder = (data) => {
        let promise = super.delete({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/folders/${data.folderId}`,
        });
        return proxy.httpResultProxy(promise);
    };
    deleteFile = (data) => {
        let promise = super.delete({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/files/${data.id}`,
            params: data,
        });
        return proxy.httpResultProxy(promise);
    };
    deleteEntries = (data) => {
        let promise = super.delete({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/entries`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    getFileInfo = (data) => {
        let promise = super.get({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/files/${data.fileId}/info`,
        });
        return proxy.httpResultProxy(promise, FileProxy);
    };
    getFolderInfo = (data) => {
        let promise = super.get({
            url: `${GET_FILES}/${data.accountId}/projects/${data.projectId}/folders/${data.folderId}/info`,
        });
        return proxy.httpResultProxy(promise, FileProxy);
    };
    postCopyFile = (data) => {
        let promise = super.post({
            url: GET_FILES + `/${data.accountId}/projects/${data.projectId}/parent/${data.id}/file`,
            data: data.body,
        });
        return proxy.httpResultProxy(promise, FileProxy);
    };
    // POST /accounts/{accountId}/projects/{projectId}/download/signedUrl
    getSigned = (data) => {
        let promise = super.post({
            url: `/accounts/${data.accountId}/projects/${data.projectId}/download/signedUrl`,
            data: data.body,
        });
        return proxy.httpResultProxy(promise);
    };
    // POST /projects/{projectId}/file/download/signedUrl 获取带有签证信息的下载文件的URL
    getFileSignedUrl = (data) => {
        let promise = super.post({
            url: `${PROJECTS}/${data.projectId}/file/download/signedUrl`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    getFolderSignedUrl = (data) => {
        let promise = super.post({
            url: `${PROJECTS}/${data.projectId}/download/signedUrl`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    detectFileExists = (data) => {
        let promise = super.post({
            url: `/file/detectFileExists`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    getFileTypes = () => {
        let promise = super.getWithoutToken({
            url: `/filetypes`,
        });
        return proxy.httpResultProxy(promise);
    };
    updateFileVersion = (data) => {
        let promise = super.put({
            url: `/accounts/${data.accountId}/projects/${data.projectId}/files/${data.fileId}/versions/${
                data.version
            }/info`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    // 文件搜索
    getSearchCondition = (data) => {
        let promise = super.get({
            url: `/accounts/${data.accountId}/files/search`,
        });
        return proxy.httpResultProxy(promise);
    };
    searchFile = (data) => {
        let promise = super.post({
            url: `/accounts/${data.accountId}/files/search`,
            data: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    // POST /accounts/{accountId}/projects/{projectId}/folder/import  导入文件夹权限
    importFolder = (data) => {
        let promise = super.post({
            url: `/accounts/${data.accountId}/projects/${data.projectId}/folder/import`,
            data: data.data,
            timeout: UPLOAD_REQUEST_TIMEOUT,
        });
        return proxy.httpResultProxy(promise);
    };
}

export {FilesService};
